<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 14:34
 */

namespace app\service;

use app\model\ProfessionalModel;
use think\Exception;

/**
 * 职称
 * Class ProfessionalService
 * @package app\service
 */
class ProfessionalService extends BaseService
{
    public $professionalModel;

    public function __construct()
    {
        $this->professionalModel = new ProfessionalModel();
    }


    /**
     * 查询列表
     * @param $where
     * @param string $field
     * @return array|bool
     */
    public function queryList($where, $field = '*')
    {
        try {
            return $this->professionalModel
                ->field($field)
                ->where($where)->select()->toArray();
        } catch (Exception $ex) {
            LogService::S()->writeLog('Exception', $ex->getMessage());
            return false;
        }
    }

    /**
     * 查询详情
     * @param $where
     * @param string $field
     * @return array|bool|false|\PDOStatement|string|\think\Model
     */
    public function find($where,$field="*"){
        try {
            $res = $this->professionalModel->where($where)->field($field)->find();
            if($res)
                $res = $res->toArray();
            return $res;
        } catch (Exception $ex) {
            LogService::S()->writeLog('Exception', $ex->getMessage());
            return false;
        }
    }


    /**
     * 添加
     * @param $add
     * @return bool
     */
    public function add($add)
    {
        try {
            return $this->professionalModel->insertGetId($add);
        } catch (Exception $ex) {
            LogService::S()->writeLog('Exception', $ex->getMessage());
            return false;
        }
    }

    /**
     * 编辑
     * @param $save
     * @param null $id
     * @return bool
     */
    public function save($save, $id = null)
    {
        try {
            if ($id) {
                return $this->professionalModel->save($save, array('id' => $id));
            } else {
                return $this->professionalModel->insertGetId($save);
            }

        } catch (Exception $ex) {
            LogService::S()->writeLog('Exception', $ex->getMessage());
            return false;
        }
    }

    /**
     * 删除
     * @param $ids
     * @param $uid
     * @return mixed
     */
    public function del($ids)
    {
        $where['id'] = array('in', $ids);
        return $this->professionalModel->where($where)->delete();
    }

    /**
     * 计数
     * @param $where
     * @return int|string
     * @throws Exception
     */
    public function count($where)
    {
        return $this->professionalModel->where($where)->count('id');
    }


}